package com.qdsg.ysg.doctor.ui;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.qdsg.ysg.doctor.util.ToastUtil;
import com.rest.business.RestProxy;
import com.rest.response.DoctorResponse;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import com.qdsg.ysg.doctor.R;
import com.qdsg.ysg.doctor.base.BaseActivity;
import com.qdsg.ysg.doctor.base.BaseApplication;

import org.jetbrains.annotations.NotNull;

/**
 * Created by Administrator on 2020/2/9.
 */

public class DepartmentListActivity extends BaseActivity {
    @BindView(R.id.refreshLayout)
    RefreshLayout refreshLayout;
    @BindView(R.id.doctor_recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.department_recyclerView)
    RecyclerView department_recyclerView;
    @BindView(R.id.edt_search)
    EditText edt_search;

    MyAdapter adapter;
    DepartmentAdapter departmentAdapter;

    List<DoctorResponse.Doctor> departmentList = new ArrayList<>();
    List<DoctorResponse.Doctor> doctorList = new ArrayList<>();


    @Override
    protected void initViews() {
        LinearLayoutManager manager = new LinearLayoutManager( mContext);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        adapter = new MyAdapter();
        recyclerView.setAdapter(adapter);

        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnRefreshListener( refreshlayout -> initData() );
        refreshLayout.setOnLoadMoreListener( refreshlayout -> {

        } );
        department_recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        departmentAdapter = new DepartmentAdapter();
        department_recyclerView.setAdapter(departmentAdapter);
        edt_search.setOnEditorActionListener( (v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {   // 按下完成按钮，这里和上面imeOptions对应
                getDepartmentList();
                department_recyclerView.setVisibility(View.VISIBLE);
                recyclerView.setVisibility(View.GONE);
            }
            return false;
        } );
    }

    @Override
    protected void initData() {
        getDepartmentList();
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.aty_department_list;
    }


    class MyAdapter extends RecyclerView.Adapter<MyAdapter.Holder> {

        @NotNull
        @Override
        public MyAdapter.Holder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
            MyAdapter.Holder myHolder;
            myHolder = new MyAdapter.Holder(LayoutInflater.from( DepartmentListActivity.this).inflate(R.layout.item_doctor, parent, false));
            return myHolder;
        }

        @SuppressLint("SetTextI18n")
        @Override
        public void onBindViewHolder(MyAdapter.Holder holder, final int position) {
            holder.itemView.setOnClickListener( v -> {
                BaseApplication.currentDoctor = doctorList.get(position);
                Bundle bundle = new Bundle();
                bundle.putSerializable("obj", doctorList.get(position));
                startActivity( PayActivity.class, bundle);
            } );
            holder.tv_department.setText(doctorList.get(position).deptName);
            holder.tv_level.setText(doctorList.get(position).jobTitleName);
            holder.tv_skill.setText("擅长:" + doctorList.get(position).docGoodAtNames);
            holder.tv_price.setText(doctorList.get(position).fee);
            holder.tv_turn_num.setText(doctorList.get(position).num);
            holder.tv_name.setText(doctorList.get(position).docName);
//            ImageLoaderHelper.getInstance().GlideImageLoader(DepartmentListActivity.this,doctorList.get(position).);
        }

        @Override
        public int getItemCount() {
            return doctorList.size();
        }

        class Holder extends RecyclerView.ViewHolder {

            TextView tv_name, tv_department, tv_level, tv_skill, tv_turn_num, tv_price;
            ImageView img_head;

            public Holder(View itemView) {
                super(itemView);
                tv_name = itemView.findViewById(R.id.tv_name);
                tv_level = itemView.findViewById(R.id.tv_level);
                tv_department = itemView.findViewById(R.id.tv_department);
                tv_skill = itemView.findViewById(R.id.tv_skill);
                tv_turn_num = itemView.findViewById(R.id.tv_turn_num);
                tv_price = itemView.findViewById(R.id.tv_price);
                img_head = itemView.findViewById(R.id.img_head);
            }
        }
    }


    class DepartmentAdapter extends RecyclerView.Adapter<DepartmentAdapter.Holder> {

        @NotNull
        @Override
        public DepartmentAdapter.Holder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
            DepartmentAdapter.Holder myHolder;
            myHolder = new DepartmentAdapter.Holder(LayoutInflater.from( mContext).inflate(R.layout.item_depaterment, parent, false));
            return myHolder;
        }

        @Override
        public void onBindViewHolder(DepartmentAdapter.Holder holder, final int position) {
            holder.itemView.setOnClickListener( v -> {
                getDoctorList(departmentList.get(position).id);
                department_recyclerView.setVisibility(View.GONE);
                recyclerView.setVisibility(View.VISIBLE);
            } );
            holder.tv_name.setText(departmentList.get(position).name);
        }

        @Override
        public int getItemCount() {
            return departmentList.size();
        }

        class Holder extends RecyclerView.ViewHolder {

            TextView tv_name;
            ImageView img_head;

            public Holder(View itemView) {
                super(itemView);
                tv_name = itemView.findViewById(R.id.tv_name);
            }
        }
    }

    private void getDepartmentList() {
        RestProxy.getInstance().getDepartmentList(edt_search.getText().toString(), new Observer<DoctorResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(DoctorResponse doctorResponse) {
                departmentList.clear();
                departmentList.addAll(doctorResponse.data);
                departmentAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError( mContext, e);
                refreshLayout.finishRefresh(false);
            }

            @Override
            public void onComplete() {
                refreshLayout.finishRefresh(true);
            }
        });
    }

    private void getDoctorList(String id) {
        RestProxy.getInstance().getDoctorList(id, new Observer<DoctorResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(DoctorResponse doctorResponse) {
                doctorList.clear();
                doctorList.addAll(doctorResponse.data);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable e) {

                ToastUtil.onError( mContext, e);
            }

            @Override
            public void onComplete() {

            }
        });
    }
}
